home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / General App Samples / QDGX Shell ƒ / put your code here.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-20  |  12.1 KB  |  417 lines  |  [TEXT/MMCC]

  1. /**\
  2. |**| =====================================================================
  3. |**|
  4. |**|    put your code here.c
  5. |**|
  6. |**|    This file contains the calls that an application needs to make
  7. |**|    the QuickDraw GX shell work correctly.
  8. |**|
  9. |**|    QuickDraw GX Libraries Used:
  10. |**|    "ColorLibrary.c", "FontLibrary.c", "GraphicsDebugLibrary.c",
  11. |**|    "ShapeLibrary.c", and "TransformLibrary.c".
  12. |**|
  13. |**|    ©1992-1996  Apple Computer, Inc.
  14. |**|    All rights reserved.
  15. |**|
  16. |**| =====================================================================
  17. \**/
  18.  
  19.  
  20. #include "QDGX shell.h"
  21.  
  22.  
  23. /**\
  24. |**| ---------------------------------------------------------------------
  25. |**| PROTOTYPES
  26. |**| ---------------------------------------------------------------------
  27. \**/
  28.  
  29. // funtions required by shell
  30.  
  31. void    DoSetup            (void);
  32. void    DoDraw            (WindowPtr wind, Boolean updating);
  33. OSErr    DoCreateNew        (void);
  34. void    DoDispose        (WindowPtr wind);
  35. void    DoIdle            (WindowPtr wind);
  36. void    DoTeardown        (void);
  37. void    DoClick            (WindowPtr wind, Point p);
  38.  
  39. // private functions
  40.  
  41. OSErr    DoWindowInit        (WindowPtr wind);
  42. void    CreateSampleImage    (WindowPtr wind);
  43.  
  44.  
  45. /**\
  46. |**| ---------------------------------------------------------------------
  47. |**| ENUMS
  48. |**| ---------------------------------------------------------------------
  49. \**/
  50. enum { rWindResource = 128 };
  51.  
  52.  
  53. /**\
  54. |**| ---------------------------------------------------------------------
  55. |**| GLOBALS
  56. |**| ---------------------------------------------------------------------
  57. \**/
  58.  
  59. // Set  "gGiveMeValidation" to TRUE if you want receive run-time validation.
  60.  
  61. Boolean        gGiveMeValidation = true;
  62.  
  63.  
  64. // gGraphicsHeapSize sets the size of the graphics heap created by calling the
  65. // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c.  You can determine
  66. // the amount of graphics heap required by using GraphicsBug.  I'm giving it 600K,
  67. // since we need abunch if we have several windows open.
  68.  
  69. long        gGraphicsHeapSize = 600;
  70.  
  71. // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
  72. // override.  This is so that our override can be native PowerPC code if necessary.
  73.  
  74. GXPrintingEventUPP    gOurPrintingOverrideUPP;
  75.  
  76.  
  77.  
  78. /**\
  79. |**| ---------------------------------------------------------------------
  80. |**| DoSetup()
  81. |**| Here's where we initialize any global variables our application needs.
  82. |**| We have only one at this time -- the universal proc pointer for
  83. |**| our printing override.
  84. |**| ---------------------------------------------------------------------
  85. \**/
  86. void DoSetup (void)
  87. {    // Initialize our printing event override UPP
  88.     gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
  89. }
  90.  
  91.  
  92. /**\
  93. |**| ---------------------------------------------------------------------
  94. |**| DoDraw()
  95. |**| Draw the contents of the window.  The first parameter is the window
  96. |**| to draw, and the second parameter is true if we're updating an existing
  97. |**| image.  If that's the case, we don't want to change anything, but
  98. |**| just draw what's already there.
  99. |**| ---------------------------------------------------------------------
  100. \**/
  101. void DoDraw (WindowPtr wind, Boolean updating)
  102. {
  103.      #pragma unused (updating)
  104.      GXDrawShape (GetDocShape(wind));
  105. }
  106.  
  107.  
  108. /**\
  109. |**| ---------------------------------------------------------------------
  110. |**| DoCreateNew()
  111. |**| This routine is called when a window needs to be created.
  112. |**| ---------------------------------------------------------------------
  113. \**/
  114. OSErr DoCreateNew (void)
  115. {
  116.     OSErr        err = noErr;
  117.     WindowPtr    wind;
  118.     
  119. // Get and create our window from the resource fork
  120.  
  121.     wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
  122.  
  123. // Attach a default gxViewPort to it, create and iInitialize our
  124. // private data for it, and add a sample image to its page shape.
  125.  
  126.     if ( wind == NULL )
  127.         return (MemError());
  128.  
  129.     GXIgnoreGraphicsNotice(transform_already_set);
  130.     SetDefaultViewPort(GXNewWindowViewPort(wind));            
  131.     GXPopGraphicsNotice();
  132.     
  133.     err = DoWindowInit(wind);
  134.     if ( err != noErr )
  135.         return err;
  136.     
  137.     CreateSampleImage(wind);
  138.     return err;
  139. }
  140.  
  141.  
  142. /**\
  143. |**| ---------------------------------------------------------------------
  144. |**| DoDispose()
  145. |**| This routine is called when a window needs to be disposed of.
  146. |**| ---------------------------------------------------------------------
  147. \**/
  148. void DoDispose (WindowPtr wind)
  149. {
  150.     TH_Doc    doc;
  151.     
  152. // You should always dispose of your GX graphics objects before tossing your window.
  153. // Why?  It's generally good form and this approach guarantees that everything is
  154. // disposed.  If you had not disposed of everything, the call to DisposeWindow should
  155. // dispose of the objects. If you are running the debugging version of QuickDraw GX
  156. // with notices set, you will receive a notice that you had not disposed of everything.
  157. // You can turn notices on in this file by not #defineing debugging.
  158.     
  159.     if ( wind != NULL )
  160.     {
  161.         doc = (TH_Doc)GetWRefCon(wind);        // Remember, this is where we stored our private data.
  162.         GXDisposeShape(GetDocShape(wind));     // Dispose of this doc's shape.
  163.         GXDisposeJob(GetDocJob(wind));        // Dispose of this doc's print job.
  164.         DisposHandle((Handle) doc);            // Dispose of our private data.
  165.         DisposeWindow(wind);                // Dispose of the window.
  166.     }
  167. }
  168.  
  169.  
  170. /**\
  171. |**| ---------------------------------------------------------------------
  172. |**| DoIdle()
  173. |**| This routine is called to do things while idling through the event loop.
  174. |**| ---------------------------------------------------------------------
  175. \**/
  176. void DoIdle (WindowPtr wind)
  177. {
  178. }
  179.  
  180.  
  181. /**\
  182. |**| ---------------------------------------------------------------------
  183. |**| DoTeardown()
  184. |**| This routine is called just before we quit to remove anything 
  185. |**| persistent that might have been setup by DoSetup().
  186. |**| ---------------------------------------------------------------------
  187. \**/
  188. void DoTeardown (void)
  189. {
  190.     DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
  191. }
  192.  
  193.  
  194. /**\
  195. |**| ---------------------------------------------------------------------
  196. |**| DoClick()
  197. |**| ---------------------------------------------------------------------
  198. \**/
  199. void DoClick(WindowPtr window, Point p)
  200. {
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207. /**\
  208. |**| ---------------------------------------------------------------------
  209. |**| DoWindowInit()
  210. |**| In this function we create and initialize the the private document
  211. |**| structure for a new window.  This structure contains the print job and
  212. |**| the shape which is drawn in the window.  We store this data in a handle
  213. |**| and hang it off the window's refCon field for easy retrieval.  By doing
  214. |**| this, rather than using globals, we can create many windows containing
  215. |**| unique print jobs and shapes.
  216. |**| ---------------------------------------------------------------------
  217. \**/
  218. OSErr DoWindowInit (WindowPtr wind)
  219. {
  220.     OSErr    err = noErr;
  221.     gxJob    docJob;
  222.     gxShape    docPage;
  223.     TH_Doc    windDoc;
  224.  
  225.  
  226. // Create the page shape. We set the unique items attribute to make sure that each item
  227. // added to the picture has a unique reference. If this attribute was not set, we would
  228. // not see all copies of anything we add to the shape multiple times -- we'd just see
  229. // the last version added.        
  230.  
  231.     docPage = GXNewShape(gxPictureType);
  232.     GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
  233.     
  234.     
  235. // Create a print job for this document.  This will be the same as the system default until
  236. // the user goes through the dialogs for Page Setup or Print…
  237.  
  238.     err = GXNewJob(&docJob);
  239.     
  240.     
  241. // If there are no errors, create a handle the size of our document structure and store
  242. // the print job and page shape in it.  Store the handle in the window's refCon field so
  243. // that we can get at it.  (Note that the utility routines "GetDocJob" and "GetDocShape"
  244. // can be used to do this easily.
  245.  
  246.     if ( err == noErr )
  247.     {
  248.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  249.  
  250.         if ( windDoc == NULL )
  251.             err = MemError();
  252.         else
  253.         {
  254.             (*windDoc)->docJob = docJob;
  255.             (*windDoc)->docPage = docPage;
  256.             SetWRefCon(wind, (long) windDoc);
  257.         }
  258.  
  259. // Now install our application override for PrintingEvent so that we can
  260. // support the new movable-modal printing dialog boxes.
  261.  
  262.         GXInstallApplicationOverride(docJob, gxPrintingEventMsg, gOurPrintingOverrideUPP);
  263.  
  264.     }
  265.  
  266.     return err;
  267. }
  268.  
  269.  
  270. /**\
  271. |**| ---------------------------------------------------------------------
  272. |**| CreateSampleImage()
  273. |**| This function creates primitive shapes and adds them to the window's page shape.
  274. |**| ---------------------------------------------------------------------
  275. \**/
  276. void CreateSampleImage (WindowPtr wind)
  277. {
  278.     gxShape            thePage;
  279.     gxShape         theLine;
  280.     gxLine            lineData = {{ff(25), ff(25)}, {ff(125), ff(125)}};
  281.     gxShape            theRect;
  282.     gxRectangle     rectData = {ff(25), ff(25), ff(75), ff(75)};    
  283.     gxShape         theCurve;
  284.     gxCurve         curveData = {{ff(25), ff(25)}, {ff(275), ff(75)}, {ff(125), ff(125)}};    
  285.     gxShape         thePath;
  286.     long             tripleEightData[] = {    1 /* # of contours */, 
  287.                                             6 /* # of points */, 
  288.                                             0xff000000,
  289.                                                0, 0,  // the points 
  290.                                                ff(75),  0, 
  291.                                                ff(5), ff(50),
  292.                                                ff(75),  ff(100),
  293.                                                0,  ff(100), 
  294.                                                ff(75), ff(50)};        
  295.     gxShape         theText;
  296.     gxRectangle     theTextBounds;
  297.     gxColor         textColor;
  298.     Fixed            x,y;
  299.     short            loop;
  300.     gxShape         thePolygon;
  301.     long starData[] = {    1,                                  // number of contours
  302.                          5 ,                                 // number of points
  303.                         ff(60), 0, ff(90), ff(90),  ff(0), ff(30),
  304.                         ff(120), ff(30), ff(0), ff(90)};     // the points
  305.  
  306.  
  307. // Retrieve the page shape so we can add to it.
  308.  
  309.     thePage = GetDocShape(wind);
  310.     
  311.     
  312. // Create a line
  313.     
  314.     theLine = GXNewLine (&lineData);
  315.  
  316.     AddToShape(thePage, theLine);
  317.     GXDisposeShape(theLine);  
  318.  
  319.  
  320. // Create a rectangle which is: red & is draw with it's frame.
  321.  
  322.     theRect = GXNewRectangle(&rectData); 
  323.      SetShapeCommonColor (theRect, red);
  324.     GXSetShapeFill (theRect, gxClosedFrameFill);
  325.     GXMoveShapeTo (theRect,  ff(150), ff(25));
  326.  
  327.     AddToShape(thePage, theRect);
  328.     GXDisposeShape(theRect);  
  329.  
  330.  
  331. // Create a curve which has: a 3.25 pen thickness
  332.     
  333.     theCurve = GXNewCurve(&curveData); 
  334.     
  335.     // The fl marco converts floating gxPoint #'s to Fixed gxPoint.
  336.     GXSetShapePen(theCurve, fl(3.25));
  337.     GXMoveShapeTo (theCurve,  ff(210), ff(25));
  338.  
  339.     AddToShape(thePage, theCurve);
  340.     GXDisposeShape(theCurve);  
  341.  
  342.  
  343. // Create apath which has: a 2 pen thickness, its color is green, and its drawn with its frame 
  344.                                                                
  345.     thePath = GXNewPaths((gxPaths *) tripleEightData);
  346.     GXSetShapeFill (thePath, gxClosedFrameFill);
  347.      GXSetShapePen(thePath, ff(2));
  348.     SetShapeCommonColor (thePath, green);
  349.  
  350.     GXMoveShapeTo (thePath,  ff(390), ff(25));
  351.  
  352.     AddToShape(thePage, thePath);
  353.     GXDisposeShape(thePath);  
  354.  
  355.  
  356. // Create a character S which is: colored in hsv space and it is rotated 15 degrees - six times
  357. // via the left bottom corner.  Create the text, set the font size, and set the font name
  358.  
  359.     theText = GXNewText(1,(unsigned char*)"S",  nil);
  360.     SetShapeCommonFont(theText, timesFont);
  361.     GXSetShapeTextSize(theText, ff(200));
  362.     GXMoveShapeTo (theText,  ff(25), ff(275));
  363.     GXSetShapeAttributes (theText,  (GXGetShapeAttributes(theText) | gxMapTransformShape));
  364.     
  365.  
  366. // Create an hsv color space and set up the initial colors
  367.  
  368.     textColor.space = gxHSVSpace;
  369.     textColor.profile = nil;
  370.     textColor.element.hsv.hue = 0x7400;
  371.     textColor.element.hsv.saturation = 0xFFFF;
  372.     textColor.element.hsv.value = 0xFFFF;
  373.  
  374.  
  375. // Get the bounds of "theText" and determine the bottom left corner
  376.  
  377.     GXGetShapeBounds(theText, 0L, &theTextBounds);
  378.     x = theTextBounds.left;
  379.     y = theTextBounds.bottom;
  380.  
  381.  
  382. // Rotate "theText" 15 degrees - 6 times. Add each letter to the picture.
  383.  
  384.     for (loop = 0; loop < 6; loop++) {
  385.         GXSetShapeColor(theText, &textColor);
  386.         GXRotateShape(theText, ff(15), x, y);
  387.     
  388.         AddToShape(thePage, theText);
  389.  
  390.         textColor.element.hsv.hue += 0x0940;
  391.     }
  392.     
  393.     GXDisposeShape(theText);  
  394.     
  395.                 
  396. // Create a polygon which has the following features: yellow, drawn with a pen = 3, and
  397. // skew it in the vertical direction by 0.5 
  398.  
  399.     thePolygon = GXNewPolygons((gxPolygons *) starData);
  400.     GXSetShapeFill(thePolygon, gxEvenOddFill);
  401.     GXSetShapePen (thePolygon, ff(3));
  402.     SetShapeCommonColor (thePolygon, yellow);
  403.     GXMoveShapeTo (thePolygon,  ff(240), ff(110));
  404.     GXSkewShape(thePolygon, 0, fl(0.5), 0, 0);
  405.     
  406.     AddToShape(thePage, thePolygon);
  407.     GXDisposeShape(thePolygon);  
  408.  
  409.  
  410. // Invalidate the window's portRect so that everything gets updated.
  411.     
  412.     SetPort(wind);
  413.     InvalRect(&wind->portRect);
  414. }
  415.  
  416.  
  417.